package com.sws.worktive.job_service;

import java.net.URLDecoder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JobListJsonParser {
	public JobData[] parseJobsJsonObject(JSONObject jsonObject){
		JobData[] JobDatas;
    	try {
			JSONArray jobsJsonArray= jsonObject.getJSONArray("jobs");
			
			JobDatas = new JobData[jobsJsonArray.length()];
			
			for (int i = 0; i < jobsJsonArray.length(); i++) {
				JSONObject jobsJsonObject = jobsJsonArray.getJSONObject(i);
				JobData JobData = new JobData();
				
				JobData.latitude = jobsJsonObject.getString(JobData.latitudeKey);
				JobData.longitude = jobsJsonObject.getString(JobData.longitudeKey);
				JobData.jobtype = jobsJsonObject.getString(JobData.jobtypeKey);
				JobData.isIamage = jobsJsonObject.getString(JobData.isImageKey);
				JobData.expireDate = jobsJsonObject.getString(JobData.expireDateKey);
				JobData.addedDate = jobsJsonObject.getString(JobData.addedDateKey);
				JobData.lastModDate = jobsJsonObject.getString(JobData.lastModDateKey);
				JobData.city = jobsJsonObject.getString(JobData.cityKey);
				JobData.state = jobsJsonObject.getString(JobData.stateKey);
				JobData.stateCode = jobsJsonObject.getString(JobData.statecodeKey);
				JobData.isContact = jobsJsonObject.getString(JobData.isContactKey);
				
				if (JobData.isIamage.equals("1")) {
					JobData.imageUrl = URLDecoder.decode(jobsJsonObject.getString(JobData.imageUrlKey));
					JobData.imageThumbnail = URLDecoder.decode(jobsJsonObject.getString(JobData.imageThumbnailKey));
				}else {
					JobData.description = jobsJsonObject.getString(JobData.descriptionKey);
				}
				
				JobDatas[i] = JobData;
			}
			return JobDatas;
			
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
    	
    }
	
	public static class JobData{
		
			public String latitude;
			public String longitude;
			public String jobtype;
			public String isIamage;
			public String expireDate;
			public String addedDate;
			public String lastModDate;
			public String description;
			public String city;
			public String state;
			public String stateCode;
			public String isContact;
			
			public String imageUrl; //= "imageUrl": "http://s52.radikal.ru/i136/0911/43/733f470e9221.png",
		    public String imageThumbnail; //= "imageThumbnail": "http://s52.radikal.ru/i136/0911/43/733f470e9221.png"
			
		    public final String latitudeKey = "latitude";
		    public final String longitudeKey = "longitude";
		    public final String jobtypeKey = "jobtype";
		    public final String isImageKey = "isImage";
		    public final String expireDateKey = "expireDate";
		    public final String addedDateKey = "addedDate";
		    public final String lastModDateKey = "lastModDate";
		    public final String descriptionKey = "description";
		    public final String cityKey = "city";
		    public final String stateKey = "state";
		    public final String statecodeKey = "statecode";
		    public final String isContactKey = "isContact";
		    public final String imageUrlKey="imageUrl";
		    public final String imageThumbnailKey="imageThumbnail";
	}
}
